﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
namespace BUS
{
    public class PhieuDatPhongBUS
    {
        public const int HetLoaiPhong = 0;
        public const int ThatBai = 1;
        public const int ThanhCong = 2;
        public static int LapPhieuDatPhong(PhieuDatPhongDTO phieuDatPhong)
        {
            bool flag = false;
            if (PhongBUS.LayDSPhong() != null)
            {
                List<PhongDTO> dsPhong = PhongBUS.LayDSPhong(-1, null, -1, phieuDatPhong.MaLoaiPhong);
                if (dsPhong.Count > 0)//tim cac phong theo loai phong duoc dat
                {
                    foreach (var p in dsPhong)
                    {
                        if (p.TrangThai == 0)
                        {
                            flag = true;
                            break;// tim thay phong trong, thoat vong lap
                        }
                       
                    }
                    if (flag == false)
                        return HetLoaiPhong;// khong tim that, het phong
                    else // tim thay phong trong, lap phieu dat
                    {
                        if (!PhieuDatPhongDAO.LapPhieuDatPhong(phieuDatPhong))
                            return ThatBai;
                        return ThanhCong;
                    }
                }
                
            }
            return HetLoaiPhong;
        }
        public static int SuaPhieuDatPhong(PhieuDatPhongDTO phieuDatPhong)
        {
            if (PhongBUS.LayDSPhong() != null)
            {
                List<PhongDTO> dsPhong = PhongBUS.LayDSPhong(-1, null, -1, phieuDatPhong.MaLoaiPhong);
                if (dsPhong.Count == 0)//tim cac phong theo loai phong duoc dat
                {
                    foreach (var p in dsPhong)
                    {
                        if (p.TrangThai == 0)
                        {
                            break;// tim thay phong trong, thoat vong lap
                        }
                        return HetLoaiPhong;// khong tim that, het phong
                    }
                }
                if (!PhieuDatPhongDAO.SuaPhieuDatPhong(phieuDatPhong))
                    return ThatBai;
                return ThanhCong;
            }
            return ThatBai;
        }
        public static List<PhieuDatPhongDTO> LayDSPhieuDatPhong(int maPhieuDatPhong = -1, string tenNguoiDat = null,int daNhanPhong = -1,int _maLoaiPhong = -1)
        {
            return PhieuDatPhongDAO.LayDSPhieuDatPhong(maPhieuDatPhong, tenNguoiDat, daNhanPhong,_maLoaiPhong);
        }
        public static bool XoaPhieuDatPhong(int _maPhieuDatPhong)
        {
            return PhieuDatPhongDAO.XoaPhieuDatPhong(_maPhieuDatPhong);
        }
    }
}
